<template>
  <div>
    <div>
      <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
        <el-breadcrumb-item :to="{ path: '/' }"><i class="el-icon-house" style="font-weight: bold">&nbsp;&nbsp;后台管理</i>
        </el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;生产管理</i></el-breadcrumb-item>
        <el-breadcrumb-item><i class="el-icon-s-marketing">&nbsp;&nbsp;设备管理</i></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="data-canRead" style="width: 100%;background-color: white;height: auto">
      <el-divider content-position="left">设备查询</el-divider>
      <el-form ref="form">
        <el-row style="vertical-align:bottom;">
          <el-col :span="1">&nbsp;</el-col>
          <el-col :span="8" style="display:inline-block;">
            <el-form-item>
              <span style="font-size: 18px;float: left">搜索设备&nbsp;</span>
              <el-input v-model="inputName" placeholder="请输入进度名称" style="width: 500px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <el-table :data="equipment.slice((currentPage-1)*pageSize,currentPage*pageSize)" stripe style="width: 100%">
        <el-table-column prop="equipmentId" label="设备ID" width="200px"></el-table-column>
        <el-table-column prop="equipmentName" label="设备名称" width="200px"></el-table-column>
        <el-table-column prop="model" label="生产单位" width="200px"></el-table-column>
        <el-table-column prop="produceDate" label="生产日期" width="200px"></el-table-column>
        <el-table-column prop="installationUnit" label="安装单位" width="200px"></el-table-column>
        <el-table-column prop="enabledDate" label="启用日期" width="200px"></el-table-column>
        <el-table-column prop="lastDetectedTime" label="上次检测时间" width="200px"></el-table-column>
        <el-table-column prop="abnormal" label="异常情况" width="200px"></el-table-column>
        <el-table-column prop="head" label="负责人" width="200px"></el-table-column>
        <el-table-column label="操作" width="200px" fixed="right">
          <template slot-scope="scope">
            <el-button
                size="mini"
                type="primary"
                circle
                v-if="display==0"
                @click="handleEdit( scope.row)"><i class="el-icon-edit" style="color: white"></i>
            </el-button>
            <el-button
                size="mini"
                type="danger"
                circle
                @click="openDeleteConfirm(scope.row)"><i class="el-icon-delete" style="color: white"></i>
            </el-button>
          </template>
        </el-table-column>
        <el-table-column></el-table-column>
      </el-table>
      <div style="margin-top:15px;">
        <el-pagination align='center' @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page="currentPage"
                       :page-sizes="[1,5,10,20]"
                       :page-size="pageSize"
                       layout="total, sizes, prev, pager, next, jumper"
                       :total="equipment.length">
        </el-pagination>
      </div>
    </div>
    <!--修改设备弹出的窗口-->
    <el-dialog title="编辑设备" :visible.sync="dialogFormVisible" append-to-body>
      <el-form :model="equipmentForm" :rules="rules" ref="equipmentForm" label-width="100px" style="margin-right: 25px">
            <el-form-item label="设备ID:" prop="equipmentId">
              <el-input v-model="equipmentForm.equipmentId" placeholder="请输入设备ID"></el-input>
            </el-form-item>
            <el-form-item label="设备名称:" prop="equipmentName">
              <el-input v-model="equipmentForm.equipmentName" placeholder="输入设备名称"></el-input>
            </el-form-item>
            <el-form-item label="生产单位:" prop="model">
              <el-input v-model="equipmentForm.model" placeholder="输入生产单位"></el-input>
            </el-form-item>
            <el-form-item label="生产日期:" prop="produceDate">
              <el-date-picker
                  v-model="equipmentForm.produceDate"
                  type="date"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  placeholder="选择生产日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="启用日期:" prop="enabledDate">
              <el-date-picker
                  style="width: 80%"
                  v-model="equipmentForm.enabledDate"
                  type="date"
                  format="yyyy 年 MM 月 dd 日"
                  value-format="yyyy-MM-dd"
                  placeholder="选择启用日期">
              </el-date-picker>
            </el-form-item>
        <el-form-item label="安装单位:" prop="installationUnit">
          <el-input v-model="equipmentForm.installationUnit" placeholder="输入安装单位"></el-input>
        </el-form-item>
            <el-form-item label="上检时间:" prop="lastDetectedTime">
              <el-date-picker
                  v-model="equipmentForm.lastDetectedTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择上次检测时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="负责人:" prop="head">
              <el-input v-model="equipmentForm.head" placeholder="输入负责人"></el-input>
            </el-form-item>
        <el-form-item label="异常情况:" prop="abnormal">
          <el-input v-model="equipmentForm.abnormal" placeholder="输入异常情况"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="warning" style="color: white" @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" style="color: white;" @click="submitAdminFrom()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogFormVisible:false,
      inputName:'',
      display:0,
      equipmentOptions: [],
      equipment: [],
      equipmentForm: {
        equipmentId: '',
        equipmentName: '',
        model: '',
        produceDate: '',
        installationUnit: '',
        enabledDate: '',
        lastDetectedTime: '',
        abnormal: '',
        head: '',
      },
      currentPage: 1,
      pageSize: 10,
      rules: {
        equipmentId: [
          {required: true, message: "请输入设备ID", trigger: 'blur'},
          {pattern: /^[0-9]+$/, message: "请输入数字", trigger: 'blur'}
        ],
        equipmentName: [
          {required: true, message: "请输入设备名称", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        model: [
          {required: true, message: "请输入生产单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        produceDate: [
          {required: true, message: "请选择生产日期", trigger: 'change'}
        ],
        installationUnit: [
          {required: true, message: "请输入安装单位", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        enabledDate: [
          {required: true, message: "请选择启用日期", trigger: 'change'}
        ],
        lastDetectedTime: [
          {required: true, message: "请选择上检时间", trigger: 'change'}
        ],
        abnormal: [
          {required: true, message: "请输入异常情况", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
        head: [
          {required: true, message: "请输入负责人", trigger: 'blur'},
          {pattern: /[\u4E00-\u9FA5]/, message: "请输入中文", trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    /*加载设备列表*/
    loadEquipmentNList() {
      let url = 'http://localhost:9080/produceEquipments';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.equipment = responseBody.data;
          this.equipmentN = this.equipment;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    /*查询材料明细*/
    searchName(keywords) {
      return this.equipment.filter(item => {
        if (item.equipmentName.includes(keywords)) {
          return item
        }
      })
    },
    /*弹出修改窗口*/
    handleEdit(equipment) {
      let url = 'http://localhost:9080/produceEquipments/' + equipment.id;
      console.log('id=' + equipment.id);
      console.log('url' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        console.log('state=' + responseBody.state);
        if (responseBody.state == 20000) {
          this.dialogFormVisible = true;
          this.equipmentForm = responseBody.data;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadEquipmentNList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    /*修改材料的窗口*/
    submitAdminFrom() {
      let url = 'http://localhost:9080/produceEquipments/' + this.equipmentForm.id + '/update';
      console.log('url' + url);
      let formData = this.qs.stringify(this.equipmentForm);
      console.log('formData=' + formData)

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            type: 'success',
            message: '修改设备成功!'
          });
          this.dialogFormVisible = false;
          this.loadEquipmentNList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadEquipmentNList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      })
    },
    /*弹出设别删除的窗口*/
    openDeleteConfirm(equipment) {
      let title = '提示';
      let message = '此操作将永久删除【' + equipment.equipmentName + '】设备, 是否继续?';
      this.$confirm(message, title, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(equipment);
      }).catch(() => {
      });
    },
    /*删除设备*/
    handleDelete(equipment) {
      let url = 'http://localhost:9080/produceEquipments/' + equipment.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除设备【' + equipment.equipmentName + '】成功！',
            type: 'success'
          });
          this.loadEquipmentNList();
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadEquipmentNList();
            }
          });
        }
      });
    },
    /*表格*/
    //每页条数改变时触发 选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pageSize = val;
    },
    //当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    },
  },
  mounted() {
    this.loadEquipmentNList();
  },
  watch: {
    inputName: function (val) {
      if (val == '') {
        this.equipment = this.equipmentN
      } else {
        this.equipment = this.searchName(val)
      }
    }
  }
}
</script>

<style scoped>
* {
  color: black;
}

.data-canRead {
  width: 380px;
  height: 380px;
  margin-top: 15px;
  box-shadow: 0px 5px 5px #8d858a;
  float: left;
  margin-right: 16px;
}

.equipment_formSubmit_search {
  width: 200px;
  float: left;
  right: 10px;
  background-color: white;
}

</style>